Skip to content

Conversation

FindHao
Copy link
Member

@FindHao FindHao commented Sep 12, 2025

Summary

  • Add first-class support for custom data types provided by the Triton repository's triton_kernels module (e.g., Tensor, Storage, StridedLayout).
  • Avoid top-level imports for optional triton_kernels.tensor by probing availability and importing lazily at use sites.
  • Update CUDA test to emit a single launch event to logs and verify:
    • Exactly one .ndjson log file is produced in the temp logs directory
    • Exactly one launch event exists in that file
    • extracted_args["X"].type == "triton_kernels.tensor.Tensor"

Motivation

  • The custom types (Tensor, Storage, StridedLayout) are defined in the Triton repo under triton_kernels and may not be installed by default. Importing them at module import time causes hard failures for users without the extra.
  • We still want to reliably validate parsing and logging by asserting that the launch event is recorded and carries the expected structured argument metadata.

Changes

tritonparse/reproducer/utils.py

  • Add top-level probe (non-import) for optional dependency:
    • TRITON_KERNELS_CUSTOM_TYPES = importlib.util.find_spec("triton_kernels.tensor") is not None
  • Add lazy loader with caching:
    • _get_triton_tensor_types() loads Tensor, Storage, StridedLayout on first use and is decorated with functools.lru_cache(maxsize=1)
  • Update _create_arg_from_info branches to:
    • Guard with if not TRITON_KERNELS_CUSTOM_TYPES: raise RuntimeError(...)
    • Import via the cached lazy loader at the point of use
    • Preserve existing tensor dtype handling for random data generation

tests/test_tritonparse.py

  • Modify test_triton_kernels_Tensor to:
    • Import from tritonparse.reproducer import utils as reproducer_utils
    • Initialize tritonparse.structured_logging with a temp logs directory
    • Run _topk_forward once to generate a single launch event
    • Read the only .ndjson file under the temp logs directory and assert:
      • Exactly one file
      • Exactly one launch event
      • extracted_args["X"].type == "triton_kernels.tensor.Tensor"
    • Synchronize CUDA and clean up the temp directory (unless TEST_KEEP_OUTPUT=1)

Backward compatibility

  • When the optional package is absent, constructing the custom types now raises a clear RuntimeError instructing that the dependency is missing. Callers can catch this or ensure the extra is installed.
  • Code paths that do not touch the custom types are unaffected.

Testing

  • Unit test updated: TestTritonparseCUDA.test_triton_kernels_Tensor
  • How to run (CUDA required):
    • python -m unittest tests.test_tritonparse -v -k test_triton_kernels_Tensor
  • Expected: test passes, asserting single launch event and correct X type in extracted_args.

Summary:
- Introduced a new utility in `tritonparse/reproducer/utils.py` for creating kernel launch arguments from a JSON file.
- Added a new test case in `tests/test_tritonparse.py` to validate the functionality of the `_topk_forward` kernel with generated arguments.
- Cleaned up imports and ensured proper logging during the test execution.
- Minor adjustments in `tritonparse/structured_logging.py` to refine type checks for Triton kernel layouts.

This update enhances the testing framework and provides a structured way to handle kernel arguments, improving overall code maintainability and test coverage.
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 12, 2025
Summary:
- Introduced a new script `.ci/install-triton-kernels.sh` to automate the installation of Triton kernels from the Triton repository.
- The script includes error handling, environment checks, and verification of the installation.
- Updated the GitHub Actions workflow in `.github/workflows/test.yml` to call the new installation script as part of the CI process.

This addition streamlines the setup process for Triton kernels, ensuring consistency and reducing manual steps during CI runs.
Summary:
- Updated `.gitignore` to include the new `repro` directory under `tests/example_output` for better management of test artifacts.
- Modified `tests/test_tritonparse.py` to reference the newly added JSON file for kernel argument generation.
- Introduced `repro_context_20250816192455.json` in `tests/example_output/repro/` to facilitate testing of the `_topk_forward` kernel with specific context data.

These changes enhance the testing framework by ensuring relevant files are tracked and accessible for test cases.
Summary:
- Changed the default Triton commit from "main" to "v3.4.0" in the `.ci/install-triton-kernels.sh` script to ensure consistency and stability in kernel installations.

This update aligns the installation process with a specific version, improving reliability during CI runs.
Summary:
- Removed direct dependency on Triton and added it as an optional dependency under the new `triton` key.
- Introduced a new optional dependency for `pytorch-triton` to specify version requirements.
- This change improves flexibility in managing dependencies while ensuring compatibility with different Triton versions.
@FindHao FindHao marked this pull request as ready for review September 13, 2025 02:00
@facebook-github-bot
Copy link
Contributor

@FindHao has imported this pull request. If you are a Meta employee, you can view this in D82361016.

@facebook-github-bot
Copy link
Contributor

@FindHao has imported this pull request. If you are a Meta employee, you can view this in D82361016.

…ests

Summary:
- Introduced a check for the presence of the `triton_kernels` module using `importlib.util`.
- Updated the `test_triton_kernels_Tensor` method to skip tests if CUDA is not available or if `triton_kernels` is not installed, enhancing test robustness.

These changes improve the reliability of the test suite by ensuring that tests are only run in appropriate environments.
@facebook-github-bot
Copy link
Contributor

@FindHao has imported this pull request. If you are a Meta employee, you can view this in D82361016.

@facebook-github-bot
Copy link
Contributor

@FindHao merged this pull request in 0c6b52c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Meta Open Source bot. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants